/*
 * 创建日期 2010-1-4
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.export;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import  org.fantasy.common.db.QryFactory;
import  org.fantasy.common.db.Query;
import  org.fantasy.common.db.bean.ReportRow;
import  org.fantasy.common.grid.bean.Param;
import  org.fantasy.common.grid.bean.ReportExportParam;
import  org.fantasy.common.grid.util.GridConst;
import  org.fantasy.common.util.DateUtil;
import  org.fantasy.common.util.Request;
import  org.fantasy.common.util.RequestUtil;
import  org.fantasy.common.util.WebUtil;

/**
 *  抽象导出类
 * @author: 王文成
 * @version: 1.0
 * @since 2010-1-4
 */
public abstract class BaseExport implements ExportSupport {

    /**
     * 导出报表
     * 
     * @param servletRequest
     * @param response
     * @throws Exception
     */
    public abstract void export(HttpServletRequest servletRequest, HttpServletResponse response) throws Exception;

    /**
     * 取得导出参数
     * 
     * @param servletRequest
     * @return
     * @throws Exception
     */
    protected ReportExportParam getExportParam(HttpServletRequest servletRequest) throws Exception {
        Request request = RequestUtil.getWrapRequest(servletRequest);
        String json = request.getParameter(GridConst.TAG_PARAM);
        ReportExportParam param = new ReportExportParam(json);
        param.setHttpServletRequest(request);
        param.setResultList(getDataList(request));
        param.prepare();
        return param;
    }

    /**
     * 取得导出的结果集
     * 
     * @return
     */
    @SuppressWarnings("all")
    protected List<ReportRow> getDataList(HttpServletRequest servletRequest) throws Exception {
        Request request = RequestUtil.getWrapRequest(servletRequest);
        QryFactory qryFactory = (QryFactory) WebUtil.getBean(request, "qryFactory");
        int pageStart = request.getInt(GridConst.PAGE_START);
        int pageSize = request.getInt(GridConst.PAGE_SIZE);
        String runtimeSql = request.getParameter(GridConst.RUNTIME_SQL);
        Query query = qryFactory.getQuery(runtimeSql);
        query.setPage(pageStart, pageSize);
        return query.report();
    }

    /**
     * 格式化文件名
     * 
     * @return
     * @throws Exception
     */
    protected String getFileName(HttpServletRequest request, Param param) throws Exception {
        String ver = request.getParameter(GridConst.BROWSER_VERSION);
        String type = request.getParameter(GridConst.EXPORT_TYPE);
        byte[] bytes = param.getExportName().getBytes("GBK");
        String encodeFileName = new String(bytes, "ISO-8859-1");
        String now = DateUtil.format(new Date(), "MMdd");
        String fileName = encodeFileName + "_" + now;
        // 如果不是IE6
        if (!ver.startsWith("6")) {
            if (type.equals(GridConst.EXPORT_TYPE_EXCEL))
                fileName += ".xls";
            else if (type.equals(GridConst.EXPORT_TYPE_HTML))
                fileName += ".html";
            else if (type.equals(GridConst.EXPORT_TYPE_TXT))
                fileName += ".txt";
        }
        return fileName;
    }
}
